package com.dungnt.vtime.alarm;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.dungnt.entity.AlarmTime;
import com.dungnt.model.Alarm;
import com.dungnt.vtime.R;
import com.dungnt.vtime.VTime;

public class AlarmFragment extends Fragment implements OnClickListener {

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public EditText getEdtHour() {
		return edtHour;
	}

	public void setEdtHour(EditText edtHour) {
		this.edtHour = edtHour;
	}

	public EditText getEdtMinute() {
		return edtMinute;
	}

	public void setEdtMinute(EditText edtMinute) {
		this.edtMinute = edtMinute;
	}

	public Button getBtnSetAlarm() {
		return btnSetAlarm;
	}

	public void setBtnSetAlarm(Button btnSetAlarm) {
		this.btnSetAlarm = btnSetAlarm;
	}

	private View view;
	private EditText edtHour, edtMinute;
	private Button btnSetAlarm;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.alarm_fragment, container, false);
		edtHour = (EditText) view.findViewById(R.id.edtHour);
		edtMinute = (EditText) view.findViewById(R.id.edtMin);
		btnSetAlarm = (Button) view.findViewById(R.id.btnSetAlarm);
		btnSetAlarm.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == btnSetAlarm) {
			Log.d("DungNT", "Set alarm");
			
			// Store to database
			AlarmTime alarmTime = new AlarmTime();
			alarmTime.setHour(Integer.parseInt(edtHour.getText().toString()));
			Log.d("DungNT", "AlarmService Hour: " + alarmTime.getHour());
			alarmTime.setMinute(Integer.parseInt(edtMinute.getText().toString()));
			Log.d("DungNT", "AlarmService Minute: " + alarmTime.getMinute());
			alarmTime.setType(AlarmTime.ALARM_TYPE_ONCE);
			Alarm alarm = new Alarm(alarmTime);
			long alarmId = alarm.persist(VTime.db);
			
			Intent service = new Intent(getActivity(), AlarmService.class);
			service.setAction(AlarmService.CREATE);
			service.putExtra(Alarm.COL_ID, alarmId);
			Log.d("DungNT", "Alarm ID: " + alarmId);
			Log.d("DungNT", "Start service");
			getActivity().getApplicationContext().startService(service);
//			getActivity().startService(service);
		}
	}
}
